<template xmlns:el-col="http://www.w3.org/1999/html">
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">
        首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>新闻管理</el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            v-model="queryInfo.query"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getNewsList"
            />
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            style="margin-bottom: 15px;margin-left: 10px"
            type="primary"
            @click="newsAdd"
          >
            增加
          </el-button>
        </el-col>
      </el-row>
      <el-table
        :data="newsList"
        style="width: 100%"
        :row-class-name="tableRowClassName"
      >
        <el-table-column
          prop="id"
          label="ID"
        />
        <el-table-column
          prop="title"
          label="新闻标题"
        />
        <el-table-column
          prop="content"
          label="新闻内容"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="category"
          label="新闻种类"
        />
        <el-table-column
          prop="author"
          label="作者"
          :show-overflow-tooltip="true"
        />
        <!--            修改按钮-->
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              :enterable="false"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                @click="updateNews(scope.row.id)"
              />
            </el-tooltip>
            <!--           删除 按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
                @click="deleteNews(scope.row.id)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>
  </div>
</template>

<script>
import { request } from '@/network/request';
// import { basePath } from '../../commom/commom';

export default {
  name: 'NewsList',
  data() {
    return {
      newsList: [],
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.getNewsList();
  },
  methods: {
    newsAdd() {
      this.$router.push({ path: '/news/add' });
    },
    updateNews(id) {
      this.$router.push({ path: `/news/edit/${id}` });
    },
    getNewsList() {
      request({
        url: '/news/list',
        params: this.queryInfo,
      }).then((res) => {
        this.newsList = res.data.list;
        this.total = res.data.total;
      });
    },
    deleteNews(id) {
      request({
        url: `/news/delete/${id}`,
        method: 'post',
      }).then((res) => {
        if (res.code !== 200) return this.$message.success('删除失败');
        this.getNewsList();
        return this.$message.success('删除成功');
      });
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getNewsList();
    },
    handleCurrentChange(newSize) {
      this.queryInfo.pageNum = newSize;
      this.getNewsList();
    },
  },
};
</script>

<style scoped>

</style>
